import Vue from 'vue'
import VueRouter from 'vue-router'

import Meau from '../views/Menu.vue'
import Train from '../views/train.vue'
import Home from '../views/Home.vue'
Vue.use(VueRouter)

const routes = [
  {
    path:'/tarinOrder',
    component:()=>import('../views/trainOrder')
  },
  {
    path:'/home',
    component:()=>import('../views/Home.vue'),
  },
  {
    path:'/gameTrain',
    component:()=>import('../views/Game.vue')
  },
  {
    path:'/setMeau',
    name:'setMeau',
    component:()=>import('../views/Set.vue')
  },
  {
    path:'/login',
    name:'login',
    component:()=>import('../views/Login.vue')
  },
  {
    path: '/meau/:user?',
    name: 'Meau',
    component: Meau
  },
  {
    path: '/train',
    name: 'Train',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: Train
  },
  {
    path: '/paitan',
    name: 'Paitan',
    component: () => import( '../views/paitan.vue')
  },
  {
    path:'/paitanTrain',
    name:'PaitanTrain',
    component:()=>import('../views/PaitanTrain.vue')
  },
  {
    path:'/zhibiao',
    name:'zhibiao',
    component:()=>import('../views/Zhibiao.vue')
  },
  {
    path:'/',
    redirect: '/home'
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  const userMsg = JSON.parse(localStorage.getItem('userMsg'))
  if(!userMsg){
    // 未登录
    if(to.path!=='/login'&&to.path!=='/home'){
      alert('尚未登录，请登录')
      next('/login')
    }else{
      next()
    }
  }else{
    next()
  }
})
export default router
